package com.ccrfid.rmc6.service.alarm;

import com.ccrfid.rmc6.entity.alarm.AlarmNotifyRecordEntity;
import com.ccrfid.rmc6.entity.sys.AccountEntity;
import com.ccrfid.rmc6.lbservice3.manager.ClientManager;
import com.ccrfid.rmc6.service.sys.AccountService;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
public class AlarmRecordServiceAspect {
    private static final Logger logger = LoggerFactory.getLogger(AlarmRecordServiceAspect.class);
    @Autowired
    private AlarmNotifyService alarmNotifyService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private ClientManager clientManager;

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.alarm.AlarmRecordService.markReadPersonalAlarmRecord(..)) && args(alarmNotifyRecordId)",
            argNames = "alarmNotifyRecordId")
    public void afterMarkReadPersonalAlarmRecord(Long alarmNotifyRecordId) {
        AlarmNotifyRecordEntity alarmNotifyRecord = alarmNotifyService.getAlarmNotifyRecordById(alarmNotifyRecordId);
        if (alarmNotifyRecord != null) {
            List<AccountEntity> accountList = accountService.getAccountListByPersonnelId(alarmNotifyRecord.getNotifyPersonnelId());
            if (accountList != null && accountList.size() > 0) {
                for (AccountEntity accountEntity : accountList) {
                    clientManager.postAlarmMsgToApp(accountEntity.getUserAccount(), alarmNotifyRecord.getNotifyPersonnelId());
                }
            }
        }
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.alarm.AlarmRecordService.markReadPersonalAlarmRecords(..)) && args(idList)",
            argNames = "idList")
    public void afterMarkReadPersonalAlarmRecords(List<Long> idList) {
        for (Long id : idList) {
            AlarmNotifyRecordEntity alarmNotifyRecord = alarmNotifyService.getAlarmNotifyRecordById(id);
            if (alarmNotifyRecord != null) {
                List<AccountEntity> accountList = accountService.getAccountListByPersonnelId(alarmNotifyRecord.getNotifyPersonnelId());
                if (accountList != null && accountList.size() > 0) {
                    for (AccountEntity accountEntity : accountList) {
                        clientManager.postAlarmMsgToApp(accountEntity.getUserAccount(), alarmNotifyRecord.getNotifyPersonnelId());
                    }
                }
            }
        }
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.alarm.AlarmRecordService.markReadAllPersonalAlarmRecords(..)) && args(userAccount)",
            argNames = "userAccount")
    public void afterMarkReadAllPersonalAlarmRecords(String userAccount) {
        AccountEntity accountEntity = accountService.getAccountInfo(userAccount);
        if (accountEntity != null && accountEntity.getPersonnelId() != null) {
            clientManager.postAlarmMsgToApp(accountEntity.getUserAccount(), accountEntity.getPersonnelId());
        }
    }
}
